import { useBasicInfoStore } from '../basicInfo/store'
import { useMeetingParamsStore } from '../meetingParams/store'
import { useUserAllStore } from '../user/all/store'
import { useMeetingVmpStore } from './store'
import { Poll, Vmp } from './interface'
import { IS_WEIBO } from '@/utils/constant'

/**切换画面合成开关*/
export const changeEnable = (enable: boolean) => {
	const { meetingType } = useMeetingParamsStore.getState()
	useMeetingVmpStore.setState({ enable: enable })
	if (enable && meetingType === 0) {
		useMeetingVmpStore.setState({ broadcast: 1 })
	}
}
/**切换模式*/
export const changeMode = (mode: number) => {
	const { meetingType } = useMeetingParamsStore.getState()
	if (mode === 2) {
		setAutotype(2)
	} else if (mode == 1) {
		setAutotype(1, meetingType === 2 ? 34 : 5)
	} else {
		setAutotype(4, meetingType === 2 ? 34 : 5)
	}
	useMeetingVmpStore.setState({ except_self: 0, mode: mode })
}
/**切换之后*/
export const setAutotype = (value: number, layout?: number) => {
	const { superMtNum } = useBasicInfoStore.getState()
	const tree = useUserAllStore.getState().tree
	if (value === 2) {
	} else if (value === 1) {
		useMeetingVmpStore.setState({ layout: layout ?? 0 })
	} else if (value === 4 || value === 3) {
		let list = tree
			.filter((x) => x.mcc.account_type !== 10 && (superMtNum ? x.mcc.interactive_mt === 1 : true))
			.map((user) => ({
				account: user.mcc.account,
				account_type: user.mcc.account_type,
				name: user.mcc.name,
				parent_id: user.mcc.parent_id ?? '',
				conf_e164: user.mcc.conf_e164 ?? '',
				portrait: user.mcc.portrait,
			}))
		useMeetingVmpStore.setState({ layout: layout ?? 0 })
		useMeetingVmpStore.getState().setBatchPollParams({ members: list })
	} else {
		return false
	}
}
export const getPollParams = () => {
	const repeat = useBasicInfoStore.getState().repeat
	const { pollPlanValue, pollPlanOptions, pollIntervalOptionsVal, pollInterval, unlimited, pollMembers, pollTimes } =
		useMeetingVmpStore.getState()
	return {
		id: '',
		mode: 1,
		name: repeat
			? ''
			: pollPlanValue === '0'
			? ''
			: pollPlanOptions.find((item) => item.value === pollPlanValue)?.label ?? '',
		keep_time: pollIntervalOptionsVal !== 0 ? pollIntervalOptionsVal : Number(pollInterval),
		num: unlimited ? 0 : Number(pollTimes),
		members: pollMembers.map((item) => {
			return {
				account: item.account,
				account_type: item.account_type,
				name: item.name,
				parent_id: item.parent_id,
				conf_e164: item.conf_e164 || item.parent_id || '',
			}
		}),
	}
}
/**获取VMP参数 预案or保存*/
export const getVmpParam = (type: 'save' | 'plan') => {
	const {
		mode,
		show_mt_name,
		voice_hint,
		except_self,
		broadcast,
		enable_custom,
		enable,
		mt_name_style,
		vmpPlanValue,
		layout,
		planOptions,
		checkVmpPollData,
		members,
	} = useMeetingVmpStore.getState()
	const _members = members.map((item) => {
		if (item.member_type === 6 && item.poll) {
			item.poll.members = item.poll.members.map((value) => {
				return Object.assign({}, value, { conf_e164: value['conf_e164'] || value['parent_id'] })
			})
		}
		return item
	})
	const { meetingType } = useMeetingParamsStore.getState()
	const repeat = useBasicInfoStore.getState().repeat
	let vmpPlanItem = planOptions.find((item) => item.value === vmpPlanValue) ?? null
	if (mode === 4 && !checkVmpPollData()) {
		if (meetingType === 0 || meetingType === 1) {
			return {
				mt_name_style: {
					font_color: '#FFFFFF',
					font_size: 1,
					position: 1,
				},
				poll: null,
				broadcast: 1,
				except_self: 0,
				id: '',
				layout: 0,
				members: [],
				mode: 2,
				enable: false,
				show_mt_name: 1,
				name: '',
				voice_hint: 1,
			}
		} else {
			return {
				mt_name_style: {
					font_color: '#FFFFFF',
					font_size: 1,
					position: 1,
				},
				poll: null,
				broadcast: 1,
				except_self: 0,
				id: '',
				layout: 34,
				members: [],
				mode: 1,
				enable: false,
				show_mt_name: 1,
				name: '',
				voice_hint: 1,
			}
		}
	} else {
		return {
			mt_name_style: {
				font_color: mt_name_style?.font_color ?? '#FFFFFF',
				font_size: mt_name_style?.font_size ?? 1,
				position: mt_name_style?.position ?? 1,
			},
			poll: mode === 3 || mode === 4 ? getPollParams() : null,
			id: type === 'save' ? '' : repeat ? '' : vmpPlanItem?.value === '0' ? '' : vmpPlanItem?.value ?? '',
			name: repeat ? '' : vmpPlanItem?.value === '0' ? '' : vmpPlanItem?.label ?? '',
			layout: meetingType === 2 ? layout ?? 0 : mode === 2 ? 0 : layout ?? 0,
			members: _members.filter((i) => !(!i.account && i.member_type === 0)),
			broadcast: meetingType === 1 || meetingType === 2 ? 1 : broadcast,
			except_self: except_self,
			mode: meetingType === 2 ? 1 : mode,
			show_mt_name: meetingType === 2 ? 0 : show_mt_name,
			voice_hint: voice_hint,
			enable: meetingType === 2 ? enable_custom ?? false : enable,
		}
	}
}
export const initReactVmp = (vmp: Vmp | null) => {
	if (vmp && JSON.stringify(vmp) !== '{}') {
		initVmp(vmp)
		initPollData(vmp.poll ?? null)
	} else {
		initNullVmp()
		initPollData(null)
	}
}
/**初始化poll数据*/
export const initPollData = (data: Poll | null) => {
	const { pollPlanOptions, update } = useMeetingVmpStore.getState()
	if (data) {
		let _name = data.name ?? ''
		let _keep_time = data.keep_time ?? 30
		let _num = data.num ?? 0
		update({
			pollPlanValue: pollPlanOptions.find((item) => item.label === _name)?.value ?? '0',
			pollTimes: _num === 0 ? '' : String(_num),
			unlimited: _num === 0,
			pollPlanName: _name,
			pollMembers: data.members,
		})
		if (_keep_time !== 15 && _keep_time !== 30 && _keep_time !== 60) {
			update({ pollIntervalOptionsVal: 0, pollInterval: String(_keep_time) })
		} else {
			update({ pollIntervalOptionsVal: _keep_time, pollInterval: '5' })
		}
		// setMembers(data.members)
	} else {
		const { superMtNum } = useBasicInfoStore.getState()
		const tree = useUserAllStore.getState().tree
		let list = tree
			.filter((x) => x.mcc.account_type !== 10 && (superMtNum ? x.mcc.interactive_mt === 1 : true))
			.map((user) => ({
				account: user.mcc.account,
				account_type: user.mcc.account_type,
				name: user.mcc.name,
				parentId: '',
			}))
		update({
			pollPlanValue: '0',
			pollInterval: '30',
			pollIntervalOptionsVal: 30,
			unlimited: true,
			pollTimes: '0',
			pollMembers: list,
		})
		// setMembers(list)
	}
}
export const initVmp = (vmp: Vmp) => {
	const { meetingType } = useMeetingParamsStore.getState()
	const { update } = useMeetingVmpStore.getState()
	update({
		mode: vmp.mode,
		poll:
			vmp.mode === 4 || vmp.mode === 3
				? {
						members: vmp?.poll?.members || [],
						keep_time: vmp?.poll?.keep_time || 30,
						num: vmp.poll?.num ?? 0,
						name: vmp.poll?.name ?? '',
						id: vmp.poll?.id || '',
						mode: vmp.poll?.mode ?? 1,
				  }
				: null,
		layout: vmp.layout ?? 5,
		broadcast: vmp.broadcast ?? 1,
		voice_hint: vmp.voice_hint ?? 0,
		show_mt_name: vmp.show_mt_name ?? 0,
		members: vmp.members ?? [],
		mt_name_style: {
			font_color: vmp.mt_name_style?.font_color ?? '#FFFFFF',
			font_size: vmp.mt_name_style?.font_size ?? 1,
			position: vmp.mt_name_style?.position ?? 1,
		},
		name: vmp.name ?? '',
		except_self: vmp.except_self ?? 1,
	})
	if (meetingType === 2) {
		update({ enable_custom: vmp.enable })
	} else {
		update({ enable: vmp.enable })
	}
}
export const initNullVmp = () => {
	const { meetingType } = useMeetingParamsStore.getState()
	const { update } = useMeetingVmpStore.getState()
	if (meetingType === 0 || meetingType === 1) {
		update({
			mt_name_style: {
				font_color: '#FFFFFF',
				font_size: 1,
				position: 1,
			},
			poll: null,
			broadcast: 1,
			except_self: 0,
			id: '',
			layout: 0,
			members: [],
			mode: 2,
			enable: IS_WEIBO ? true : false,
			show_mt_name: 1,
			name: '',
			voice_hint: 1,
		})
	} else {
		update({
			mt_name_style: {
				font_color: '#FFFFFF',
				font_size: 1,
				position: 1,
			},
			poll: null,
			broadcast: 1,
			except_self: 0,
			id: '',
			layout: 34,
			members: [],
			mode: 1,
			enable: IS_WEIBO ? true : false,
			name: '',
			voice_hint: 1,
			enable_custom: false,
		})
	}
}
export const meetingTypeChangeListener = (_meetingType: number, meetingType: number) => {
	const { update, voice_hint, show_mt_name, mode, broadcast, except_self, layout } = useMeetingVmpStore.getState()
	if ((_meetingType === 1 || _meetingType === 0) && meetingType === 2) {
		update({
			layout: 34,
			mode: 1,
			voice_hint: voice_hint,
			_meetingType: meetingType,
		})
	} else if (_meetingType === 2 && (meetingType === 1 || meetingType === 0)) {
		update({
			show_mt_name: 1,
			mode: 2,
			layout: 5,
			broadcast: 1,
			voice_hint: voice_hint,
			except_self: 0,
			_meetingType: meetingType,
		})
	} else if ((_meetingType === 0 || _meetingType === 1) && (meetingType === 1 || meetingType === 0)) {
		update({
			show_mt_name: show_mt_name ?? 0,
			mode: mode,
			layout: layout,
			broadcast: broadcast,
			voice_hint: voice_hint,
			except_self: except_self,
			_meetingType: meetingType,
		})
		return
	}
}
